﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DC2010_EditorInfo
{
    public partial class GfxPlacementHelpDialog : Form
    {
        public int X { get; set; }
        public int Y { get; set; }

        public int PosX { get; set; }
        public int PosY { get; set; }

        public string Pth { get; set; }
        public int Scl { get; set; }


        public GfxPlacementHelpDialog(int x, int y, int posX, int posY, string path, int scale)
        {
            InitializeComponent();

            X = x;
            Y = y;

            PosX = posX;
            PosY = posY;

            Pth = path;
            Scl = scale;

            numericUpDown1.Value = posX;
            numericUpDown2.Value = posY;
            numericUpDown3.Value = scale;


        }

        private void GfxPlacementHelpDialog_Load(object sender, EventArgs e)
        {
            textBox1.Text = Pth;

            string path = Path.Combine(Environment.CurrentDirectory, constructPathToImage());
            if (File.Exists(path))
            {
                pictureBox1.Image = Image.FromFile(path);
            }

            pictureBox2.Left = PosX;
            pictureBox2.Top = PosY;

            pictureBox2.Visible = false;
        }

        private string constructPathToImage()
        {
            string res = "";

            if (IsWallCB.Checked) res += "Wall_";


            if (X < 0) res += "m";
            res +=Math.Abs(X)+"_";
            
            if (Y < 0) res += "m";
            res += Math.Abs(Y);

            string prefix = @"Data\Gfx\Editor\" + res+".png";
            return prefix;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            PosX = (int)numericUpDown1.Value;
            PosY = (int)numericUpDown2.Value;
            Scl = (int)numericUpDown3.Value;

            pictureBox2.Left = PosX;
            pictureBox2.Top = PosY;

            pictureBox1.Refresh();
        }

        Image i = null;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string s = Path.Combine(Environment.CurrentDirectory, textBox2.Text);
            string s2 = Path.Combine(s, textBox1.Text);
            s2 += ".png";

            if (File.Exists(s2))
            {
                i = Image.FromFile(s2);
                pictureBox2.Width = i.Width;
                pictureBox2.Height = i.Height;

                pictureBox1.Refresh();
            }

            Pth = textBox1.Text;
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            int doubleMultiPlier = 1;
            if (checkBox1.Checked) doubleMultiPlier = 2;

            if (i != null)
                e.Graphics.DrawImage(i, (int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)((i.Width * doubleMultiPlier) * ((double)Scl / 100)), (int)(i.Height * ((double)Scl / 100)));
        }

        private void IsWallCB_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDown2_ValueChanged(null, null);
        }

        private void GfxPlacementHelpDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            numericUpDown2_ValueChanged(null, null);
        }
    }
}
